package com.lancoo.cvas.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @ClassName AuthenticationFilter
 * @Description TODO
 * Authorization string 登录token值 //
 * userId        string    请求用户Id  //
 * userName    string    用户名  //
 * sysId        string    调用子系统ID //
 * <p>
 * ps: 若后台请求无法获取token值，则只传sysid,Authorization。 Authorization传:sysId的MD5并反转值且userId、userName 不传
 * @Author quin
 * @Date 2023/2/21 13:48
 */
@Component
@Slf4j
public class AuthenticationFilter implements HandlerInterceptor {

//    @Resource
//    private YunBasicService yunBasicService;


    /**
     * 覆盖父类的方法，
     * 如果返回true则可以调用方法，
     * 如果返回false则不调用
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//
//        if (!(handler instanceof HandlerMethod)) {
//            return true;
//        }
//
//
//        //请求带上令牌，Authorization:Bearer token
//        String authorization = request.getHeader("Authorization");
//        String sysId = request.getHeader("sysId");
//        String userId = request.getHeader("userId");
//        String userName = request.getHeader("userName");
//
//
//        if (StringUtils.isNotBlank(authorization) && StringUtils.isNotBlank(sysId) && StringUtils.isNotBlank(userId) && StringUtils.isNotBlank(userName)) {
//            //用户名 token验证
//            UserInfo userInfoByToken = yunBasicService.getUserInfoByToken(authorization);
//
//            if (userInfoByToken != null && userId != null && userName != null) {
//                //前端访问时
//                if (userInfoByToken.getUserID() == null || userInfoByToken.getUserName() == null) {
//                    userInfoByToken.setUserID("");
//                    userInfoByToken.setUserName("");
//                }
//                if (userInfoByToken.getUserID().equalsIgnoreCase(userId) && userInfoByToken.getUserName().equalsIgnoreCase(URLDecoder.decode(userName, "utf-8"))){
//                    return true;
//                }
//            }
//        } else if (StringUtils.isNotBlank(authorization) && StringUtils.isNotBlank(sysId)) {
//            //后台系统访问
//            String Md5_AppId = DigestUtils.md5DigestAsHex(sysId.getBytes());
//            StringBuffer sb = new StringBuffer(Md5_AppId);
//            String strAccess_token = sb.reverse().toString();
//            if (authorization.equals(strAccess_token)) {
//                return true;
//            }
//        }
//
//        Result<Object> responseData = Result.ofFail(EnumResultCode.INVALID_TOKEN);
//        response.setContentType("text/html;charset=UTF-8");
//        response.getWriter().write(JSON.toJSONString(responseData));
//        return false;
//
////        log.error("==================请求方式：1"  + " 请求地址： " + request.getRequestURI() );
////        log.error("==================authorization: " + authorization + " sysId: " + sysId + " userId: " + userId + " userName: " + userName);
//
////        response.setContentType("application/json;charset=UTF-8");
////        //设置编码格式
////        response.setCharacterEncoding("UTF-8");
////        response.setStatus(401);
////        response.getWriter().write("未通过鉴权认证");
////        return false;
//    }










//    public void returnErrorResponse(HttpServletResponse response, JsonResult result) {
//        OutputStream out = null;
//
//        try {
//            response.setCharacterEncoding("utf-8");
//            response.setContentType("text/json");
//            out = response.getOutputStream();
//            out.write(JsonUtils.objectToJson(result).getBytes("utf-8"));
//            out.flush();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                if (out != null) {
//                    out.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//
//    }
}
